package com.timemanage.activity.ui.home

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.Animation
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.timemanage.R
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BottomPopupView
import com.lxj.xpopup.util.XPopupUtils
import com.timemanage.dao.AppDatabase
import com.timemanage.dao.UserDao
import com.timemanage.entity.User
import com.timemanage.entity.Work
import com.timemanage.utils.MyApplication
import kotlinx.android.synthetic.main.fragment_home.*
import kotlinx.android.synthetic.main.sort_add.view.*
import kotlin.concurrent.thread
import android.content.Intent
import com.timemanage.activity.BottomBar

import java.io.File
import java.net.URL
import java.nio.file.OpenOption

import android.content.BroadcastReceiver

import android.content.IntentFilter

import androidx.localbroadcastmanager.content.LocalBroadcastManager





class HomeFragment : Fragment() {
    lateinit var add_sort_button: FloatingActionButton
    var isGetData:Boolean = false
    override fun onCreateAnimation(transit: Int, enter: Boolean, nextAnim: Int): Animation? {
        isGetData = enter && !isGetData
        return super.onCreateAnimation(transit, enter, nextAnim)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_home,container,false)
        add_sort_button = view.findViewById(R.id.addSort_Button)
        return view
    }
    var isPause = false
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        var sortName = ArrayList<String>()
        var sortBody = ArrayList<String>()
        var sortId   = ArrayList<String>()
        var database = AppDatabase.getDatabase(MyApplication.context)
        var workDao = database.WorkDao()
        val sorts = workDao.selectMainType()
        for (i in sorts) {
            sortName.add(i.title!!)
            sortBody.add(i.workbody!!)
            sortId.add(i.workId.toString())
        }

        val sortsAdapter = context?.let { sortAdapter(it, sortId,sortName, sortBody) }
        recycleView.adapter = sortsAdapter
        recycleView.layoutManager = LinearLayoutManager(context)
        //sortsAdapter?.notifyDataSetChanged()

        add_sort_button.setOnClickListener{
            XPopup.Builder(context)
                .enableDrag(true)
                .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                .asCustom(context?.let { it1 -> addSort(it1) } /*.enableDrag(false)*/)
                .show()
        }

        val broadcastManager = LocalBroadcastManager.getInstance(requireActivity())
        val intentFilter = IntentFilter()
        intentFilter.addAction("android.intent.action.CART_BROADCAST")
        val mItemViewListClickReceiver: BroadcastReceiver = object : BroadcastReceiver() {
            override fun onReceive(context: Context, intent: Intent) {
                val msg = intent.getStringExtra("data")
                if ("refresh" == msg) {
                    refresh()
                }
            }
        }
        broadcastManager.registerReceiver(mItemViewListClickReceiver, intentFilter)
    }

    inner class addSort(context: Context) : BottomPopupView(context) {
        override fun getImplLayoutId(): Int {
            return R.layout.sort_add
        }

        override fun onCreate() {
            super.onCreate()
            add_button.setOnClickListener {
                val sortName = sort_title_input.text.toString()
                val sortBody = sort_body_input.text.toString()
                var database = AppDatabase.getDatabase(MyApplication.context)
                var userdao: UserDao = database.userDao();
                var workDao = database.WorkDao()
                val userid = userdao.selectAcUser().userId
                var work1: Work? = Work(userid, -1, 1, sortName, sortBody, null, null)
                workDao.insertWork(work1)
                refresh()
                dismiss()
            }
        }
        override fun getMaxHeight(): Int {
            return (XPopupUtils.getScreenHeight(context)*.85f).toInt()
        }
    }

    fun refresh(){
        var database = AppDatabase.getDatabase(MyApplication.context)
        var workDao = database.WorkDao()
        val sorts = workDao.selectMainType()
        var sortName = ArrayList<String>()
        var sortBody = ArrayList<String>()
        var sortId   = ArrayList<String>()
        for (i in sorts) {
            sortName.add(i.title!!)
            sortBody.add(i.workbody!!)
            sortId.add(i.workId.toString())
        }
        val sortsAdapter = context?.let { sortAdapter(it, sortId,sortName, sortBody) }
        recycleView.adapter = sortsAdapter
        recycleView.layoutManager = LinearLayoutManager(context)
    }

}
